package semantic.rules.walk

import syntax.{CGrammar, CASTNode}
import CGrammar._
import scala.collection.mutable.Buffer
import utils.data.CTree
import semantic.COperations._
import semantic.CLabels._

object CWalkRuleWhile extends CWalkRule
{
    override val categories = CWhile :: Nil

    override def pre(node:CASTNode)(implicit tree:CTree[CASTNode], code:Buffer[String]) =
        if (code.isEmpty) code += s"$LABEL_WHILE_BEGIN${node id}"
        else code(code.size - 1) += s"$LABEL_WHILE_BEGIN${node id}"

    override def postChild(node:CASTNode, child:CASTNode)(implicit tree:CTree[CASTNode], code:Buffer[String]) =
        if (node.category == CWhile && child.category == CExpression) code += s"$PUSH $LABEL_WHILE_TARGET${node id}" += IF

    override def post(node:CASTNode)(implicit tree:CTree[CASTNode], code:Buffer[String]) =
        code += s"$PUSH $LABEL_WHILE_GO_TO${node id}" += GO_TO
}
